<%@ page import="com.service.bookService" %>
<%@ page import="java.io.PrintWriter" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="com.util.dbUtil" %>
<%@ page import="com.pojo.book" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%@ page import="com.service.userService" %><%--
  Created by IntelliJ IDEA.
  User: 游政杰
  Date: 2020/11/5
  Time: 17:47
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>购物车</title>
</head>
<style type="text/css">

    body{
        background-color: antiquewhite;
    }




</style>
<body>
<%
    bookService bookService =new bookService();
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter pt = response.getWriter();
    String bookid = request.getParameter("bookid");
    ResultSet resultSet = bookService.queryOne(bookid);
    Cookie[] cookies = request.getCookies();
    userService userService=new userService();

%>

<%
    pt.print("我要<a href=\"index.jsp\">继续购买</a><br/>");
    pt.print("<table border=\"1\">");
    pt.print("<tr><td colspan=\"5\" align=\"center\">您要购买的书是：</td></tr>");
    pt.print("<tr><td>图书编号</td><td>图书名称</td><td>单价</td><td>本数</td></tr>");
    while (resultSet.next()){
        pt.print("<tr>");
        pt.print("<td>"+resultSet.getString(1)+"</td>");
        pt.print("<td>"+resultSet.getString(2)+"</td>");
        pt.print("<td>"+resultSet.getDouble(3)+"</td>");
        pt.print("<form action=\"AddSuccessServlet\" method=\"POST\">");
        pt.print("<td> <input type=\"text\" width=\"50px\" name=\"bookCount\">  <input type=\"hidden\" name=\"bookid\" value=\""+resultSet.getString(1)+"\">   <input type=\"submit\" value=\"提交\"></input></td>");

        pt.print("</form>");

        pt.print("<tr/>");
    }
    pt.print("</table>");



    pt.print("<table border=\"1\" width=\"750px\">");

    pt.print("<tr><td colspan=\"7\" align=\"center\">您的购物车：</td></tr>");
    double result=0.0;
    pt.print("<tr><td>序号&#160</td> <td>图书名称&#160&#160&#160&#160&#160&#160</td> <td>数目&#160&#160&#160&#160</td> <td>单价&#160&#160&#160</td> <td>总价&#160&#160&#160</td> <td>删除&#160&#160&#160</td> <td>减少&#160&#160&#160</td>  </tr><br/>");
    Map<String, List<book>> car = (Map<String, List<book>>) session.getAttribute("car");
    String  uid=null;
    for (int i = 0; i < cookies.length; i++) {
        if(cookies[i].getName().equals("loginUser")){
            uid=cookies[i].getValue();
        }
    }
    List<book> buycar = car.get(uid);
    if(car!=null&&car.size()>0){ //此处优先判空，再判断大小，否则可能会nullpointException，**顺序不能变
        for (int i = 0; i < buycar.size(); i++) {
            result+=buycar.get(i).getPrice()*buycar.get(i).getBookcount();
            pt.print("<tr>");
            pt.print("<td>"+buycar.get(i).getBookid()+"</td>");
            pt.print("<td>"+buycar.get(i).getBookName()+"</td>");
            pt.print("<td>"+buycar.get(i).getBookcount()+"</td>");
            pt.print("<td>"+buycar.get(i).getPrice()+"</td>");
            pt.print("<td>"+buycar.get(i).getPrice()*buycar.get(i).getBookcount()+"</td>");
            pt.print("<td><a href=\"deleteServlet?bookid="+buycar.get(i).getBookid()+"&page=1\">从购物车中移除</a></td>");
            pt.print("<td>");
            pt.print("<form action=\"decrServlet\" method =\"post\" id=f"+i+">");
            pt.print(" <input type=\"hidden\" name=\"page\" value=\"1\">  <input type=\"text\" name=\"decr\"> </input> <input type=\"hidden\" name=\"bookid\" value=\""+buycar.get(i).getBookid()+"\"> </input>   <input type =\"submit\" value=\"提交\" id=s"+i+">");
            pt.print("</form>");
            pt.print("</td>");
            pt.print("</tr>");
        }
        pt.print("<tr><td colspan=\"4\">总价为：</td><td>"+result+"</td><td></td><td></td></tr>");

    }
    pt.print("</table>");


//    userService.getUserMoney(uid)
    pt.print("");

    pt.print("<a href=\"purchaseServlet\">去结算("+buycar.size()+")</a>");



%>







</body>
</html>
